#ifndef ECU_MC33771_H_
#define ECU_MC33771_H_


#define Short_GND			0.3
#define Short_POWR			4.9
#define Short_GND_AD			((Short_GND/5)*32767)
#define Short_POWR_AD			((Short_POWR/5)*32767)


#define MSGLEN			5						/*MC33771指令长度单位字节： 5个字节*/
#define GLOBAL_CID      0						/*!< CID for global write commands*/
#define TAG_CID        	1
#define BIT(x)            (1<<x)

/* VPWR resolution          	2.48mV*/
/* Vres (15bit)					152uV*/
#define Q16_16_VPWR_RES     	(u32)(2.48*0x10000)
#define Q16_16_VCELL        	(u32)(0.152*0x10000)
#define MC33771_DATA_RDY    	(BIT(15))
#define MC33771_SOFT_RST		(BIT(4))
#define MC33771_GO2SLEEP    	(BIT(0))


typedef enum
{
	CmdNOP     = 0x0,
	CmdRdLocal = 0x1,
	CmdWrLocal = 0x2,
	CmdWrGlobal= 0x3
}TYPE_CMD;

typedef enum
{
	Reserved      	= 0x00,
	INIT          	= 0x01,			/*device initialization	   		*/
	SYS_CFG_GLOBAL	= 0x02,			/* global system configuration	*/
	SYS_CFG1      	= 0x03,			/* system configuration			*/
	SYS_CFG2      	= 0x04,			/* system configuration			*/
	SYS_DIAG      	= 0x05,			/* system diagnostics			*/
	ADC_CFG       	= 0x06,			/* ADC configuration			*/
	ADC2_OFFSET_COMP= 0x07,   		/* ADC2 offset compensation		*/
	OV_UV_EN        = 0x08,			/* CT measurement selection		*/
	CELL_OV_FLT 	= 0x09,			/* CT overvoltage fault			*/
	CELL_UV_FLT 	= 0x0A,			/* CT undervoltage fault		*/
	/*Reserved        0x0B*/
	CB1_CFG			= 0x0C,			/* CB configuration for cell 1	*/
	CB2_CFG			= 0x0D,			/* CB configuration for cell 2	*/
	CB3_CFG			= 0x0E,			/* CB configuration for cell 3	*/
	CB4_CFG			= 0x0F,			/* CB configuration for cell 4	*/
	CB5_CFG			= 0x10,			/* CB configuration for cell 5	*/
	CB6_CFG			= 0x11,			/* CB configuration for cell 6	*/
	CB7_CFG			= 0x12,			/* CB configuration for cell 7	*/
	CB8_CFG			= 0x13,			/* CB configuration for cell 8	*/
	CB9_CFG			= 0x14,			/* CB configuration for cell 9	*/
	CB10_CFG		= 0x15,			/* CB configuration for cell 10	*/
	CB11_CFG		= 0x16,			/* CB configuration for cell 11	*/
	CB12_CFG		= 0x17,			/* CB configuration for cell 12	*/
	CB13_CFG		= 0x18,			/* CB configuration for cell 13	*/
	CB14_CFG		= 0x19,			/* CB configuration for cell 14	*/
	CB_OPEN_FLT 	= 0x1A,			/* Open CB fault				*/
	CB_SHORT_FLT 	= 0x1B,			/* Short CB fault				*/
	CB_DRV_STS		= 0x1C,			/* CB driver status				*/
	GPIO_CFG1 		= 0x1D,			/* GPIO configuration			*/
	GPIO_CFG2		= 0x1E,			/* GPIO configuration			*/
	GPIO_STS 		= 0x1F,			/* GPIO diagnostic				*/
	AN_OT_UT_FLT 	= 0x20,			/* AN over and undertemp		*/
	GPIO_SHORT_Anx_OPEN_STS = 0x21, /* Short GPIO / Open AN diagnostic*/
	I_STATUS		= 0x22, 		/* This register contains PGA DAC value*/
	COM_STATUS    	= 0x23,     	/* Number of CRC error counted*/
	FAULT1_STATUS 	= 0x24,	  		/* Fault status			*/
	FAULT2_STATUS 	= 0x25,	  		/* Fault status			*/
	FAULT3_STATUS 	= 0x26,	  		/* Fault status			*/
	FAULT_MASK1   	= 0x27,	  		/* Fault pin mask		*/
	FAULT_MASK2   	= 0x28,	  		/* Fault pin mask		*/
	FAULT_MASK3   	= 0x29,	  		/* Fault pin mask		*/
	WAKEUP_MASK1  	= 0x2A,	  		/* Wakeup events mask	*/
	WAKEUP_MASK2  	= 0x2B,	  		/* Wakeup events mask	*/
	WAKEUP_MASK3  	= 0x2C,	  		/* Wakeup events mask	*/
	CC_NB_SAMPLES 	= 0x2D,	  		/* Number of samples in coulomb counter*/
	COULOUMB_CNT1 	= 0x2E,     	/* Couloumb counter accumulator*/
	COULOUMB_CNT2 	= 0x2F, 	
	MEAS_ISENSE1  	= 0x30,     	/* ISENSE measurement			*/
	MEAS_ISENSE2  	= 0x31,		    /* ISENSE measurement			*/
	MEAS_STACK    	= 0x32,     	// stack  voltage measurement	*/
	MEAS_CELL14   	= 0x33,			/* Cell14 voltage measurement	*/
	MEAS_CELL13   	= 0x34,     	/* Cell13 voltage measurement	*/
	MEAS_CELL12   	= 0x35,     	/* Cell12 voltage measurement	*/
	MEAS_CELL11   	= 0x36,     	/* Cell11 voltage measurement	*/
	MEAS_CELL10   	= 0x37,     	/* Cell10 voltage measurement	*/
	MEAS_CELL9    	= 0x38,     	/* Cell9  voltage measurement	*/
	MEAS_CELL8    	= 0x39,     	/* Cell8  voltage measurement	*/
	MEAS_CELL7    	= 0x3A,     	/* Cell7  voltage measurement	*/
	MEAS_CELL6    	= 0x3B,     	/* Cell6  voltage measurement	*/
	MEAS_CELL5    	= 0x3C,     	/* Cell5  voltage measurement	*/
	MEAS_CELL4    	= 0x3D,     	/* Cell4  voltage measurement	*/
	MEAS_CELL3    	= 0x3E,     	/* Cell3  voltage measurement	*/
	MEAS_CELL2    	= 0x3F,     	/* Cell2  voltage measurement	*/
	MEAS_CELL1    	= 0x40,     	/* Cell1  voltage measurement	*/
	
	
	MEAS_AN6 = 0x41, 				/* AN6 voltage measurement		*/
	MEAS_AN5 = 0x42, 				/* AN5 voltage measurement		*/
	MEAS_AN4 = 0x43, 				/* AN4 voltage measurement		*/
	MEAS_AN3 = 0x44, 				/* AN3 voltage measurement		*/
	MEAS_AN2 = 0x45, 				/* AN2 voltage measurement		*/
	MEAS_AN1 = 0x46, 				/* AN1 voltage measurement		*/
	MEAS_AN0 = 0x47,  				/* AN0 voltage measurement		*/
	MEAS_IC_TEMP =0x48, 			/* IC temperature measurement	*/
	MEAS_VBG_DIAG_ADC1A =0x49, 		/* ADCIA Band Gap Reference measurement	*/
	MEAS_VBG_DIAG_ADC1B = 0x4A, 	/* ADCIA Band Gap Reference measurement	*/
	TH_ALL_CT = 0x4B,	 			/* CTx over and undervoltage threshold	*/
	TH_CT14 = 0x4C, 				/* CT14 over and undervoltage threshold	*/
	TH_CT13 = 0x4D, 				/* CT13 over and undervoltage threshold	*/
	TH_CT12 = 0x4E, 				/* CT12 over and undervoltage threshold	*/
	TH_CT11 = 0x4F, 				/* CT11 over and undervoltage threshold	*/
	TH_CT10 = 0x50, 				/* CT10 over and undervoltage threshold	*/
	TH_CT9  = 0x51, 				/* CT9 over and undervoltage threshold	*/
	TH_CT8  = 0x52, 				/* CT8 over and undervoltage threshold	*/
	TH_CT7  = 0x53, 				/* CT7 over and undervoltage threshold	*/
	TH_CT6  = 0x54, 				/* CT6 over and undervoltage threshold	*/
	TH_CT5  = 0x55, 				/* CT5 over and undervoltage threshold	*/
	TH_CT4  = 0x56, 				/* CT4 over and undervoltage threshold	*/
	TH_CT3  = 0x57, 				/* CT3 over and undervoltage threshold	*/
	TH_CT2  = 0x58, 				/* CT2 over and undervoltage threshold	*/
	TH_CT1  = 0x59, 				/* CT1 over and undervoltage threshold	*/
	TH_AN6_OT = 0x5A, 				/* AN6 overtemp threshold	*/
	TH_AN5_OT = 0x5B, 				/* AN5 overtemp threshold	*/
	TH_AN4_OT = 0x5C, 				/* AN4 overtemp threshold	*/
	TH_AN3_OT = 0x5D, 				/* AN3 overtemp threshold	*/
	TH_AN2_OT = 0x5E, 				/* AN2 overtemp threshold	*/
	TH_AN1_OT = 0x5F, 				/* AN1 overtemp threshold	*/
	TH_AN0_OT = 0x60, 				/* AN0 overtemp threshold	*/
	TH_AN6_UT = 0x61, 				/* AN6 undertemp threshold	*/
	TH_AN5_UT = 0x62, 				/* AN5 undertemp threshold	*/
	TH_AN4_UT = 0x63, 				/* AN4 undertemp threshold	*/
	TH_AN3_UT = 0x64, 				/* AN3 undertemp threshold	*/
	TH_AN2_UT = 0x65, 				/* AN2 undertemp threshold	*/
	TH_AN1_UT = 0x66, 				/* AN1 undertemp threshold	*/
	TH_AN0_UT = 0x67, 				/* AN0 undertemp threshold	*/
	/*Reserved  = 0x68,*/ 			/* Reserved					*/
	TH_ISENSE_OC = 0x69, 			/* ISENSE over current threshold	*/
	/*Reserved  = 0x6A,*/ 			/* Reserved Not Readable or Writeable	*/
	SILICON_REV   = 0x6B,     		/* silicon revision	*/
	EEPROM_CNTL   = 0x6C,     		/* EEPROM transfere control	*/
	DED_ENCODE1   = 0x6D,     		/* ECC signature 1	*/
	DED_ENCODE2   = 0x6E,     		/* ECC signature 2	*/
	
}REG_NAME_TYPE;

typedef struct
{
	u16 u16ValidFlags;				/* 	1: valid  0:invalid (state of Data ready signal)	*/
	u16 u16VoltCell[22];			/*	[0] is stack voltage [1]...[14] cell1..14	// +7	*/
	u16 u16RawCell[22];				/*	raw voltage reading							// +7	*/
}TYPE_BatteryPack;

typedef struct
{
	u8	u8_DataL;
	u8	u8_DataH;
	u8  u8_Addr;
	u8  u8_CID_Cmd;
	u8  u8CRC;
}MC33771_CMD_FLG;

void ECU_MC33771_RE_INIT(void);
u8 ECU_MC33771_REST(void);
/********************************************************
**函数名：	ECU_MC33771_PORT_INIT
**功能：	MC33771相关硬件模块初始化
**输入：	无
**输出：	无
**注意：	根据实际硬件电路确认通道
*********************************************************/
void ECU_MC33771_PORT_INIT(void);
void ECU_MC33771_Pack(MC33771_CMD_FLG Mc33771_W_Flg);
void ECU_MC33771_DMA_START(void);
void ECU_MC33771_INIT(void);

void ECU_MC33771_Normal(void);

bool ECU_MC33771_READY_STS(void);
void ECU_MC33771_CCELL_VCT(u16* P_u16_Value);


void ECU_MC33771_Clear_Flt(void);

void ECU_MC33771_GVal_SttCov(void);

void ECU_MC33771_Blance(u32 u32_Tx_Addr,u8 u8_Lenth);
void ECU_MC33771_CMD(u32 u32_Tx_Addr,u8 u8_Lenth);
u8 ECU_MC33771_ERR_Check(void);



void ECU_MC33771_DMA3(void);




/********************************************************
**函数名：	ECU_MC33771_ClltCellVol
**功能：	计算MC33771采集的单体电压值
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位V
*********************************************************/
extern float ECU_MC33771_ClltCellVol(u16 u16_Cell_Ad);

/********************************************************
**函数名：	ECU_MC33771_ClltCurr
**功能：	计算MC33771采集的电流值
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位A	    采样电阻100μΩ
*********************************************************/
extern float ECU_MC33771_ClltCurr(u16 u16_Cell_Ad1,u16 u16_Cell_Ad2);

/********************************************************
**函数名：	ECU_MC33771_BlanceTemp
**功能：	计算MC33771采集的TEMP温度
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位℃
*********************************************************/
extern float ECU_MC33771_BlanceTemp(u16 u16_Cell_Ad);

/********************************************************
**函数名：	ECU_MC33771_ClltTemp
**功能：	计算MC33771采集的TEMP温度
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位℃
*********************************************************/
extern float ECU_MC33771_ClltTemp(u16 u16_Cell_Ad);

/********************************************************
**函数名：	ECU_MC33771_ClltPackVol
**功能：	计算MC33771采集的PACK包总电压值
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位V
*********************************************************/
extern float ECU_MC33771_ClltPackVol(u16 u16_Cell_Ad);

/********************************************************
**函数名：	ECU_MC33771_ClltPackVol
**功能：	计算MC33771采集的PACK包总电压值
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位V
*********************************************************/
extern float ECU_MC33771_ClltFuseVol(u16 u16_Cell_Ad);

/********************************************************
**函数名：	ECU_MC33771_ClltRelayVol
**功能：	计算MC33771采集的继电器电压值
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位V
*********************************************************/
extern float ECU_MC33771_ClltRelayVol(u16 u16_Cell_Ad);

/********************************************************
**函数名：	ECU_MC33771_IC_Temp
**功能：	计算MC33771采集的IC温度
**输入：	MC33711采集的AD值
**输出：	浮点型电压值
**注意：	单位℃
*********************************************************/
extern float ECU_MC33771_IC_Temp(u16 u16_Cell_Ad);

#endif
